﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;
using System.Web;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together.

public class Service : IService
{
    public ItemInfo[] ReadItems(int parent)
    {
        CItem item = new CItem();
        DataTable dt = item.ReadItems(parent);
        if (null != dt)
        {
            ItemInfo[] infos = new ItemInfo[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ItemInfo info = new ItemInfo();
                info.ID = Convert.ToInt32(dt.Rows[i]["id"].ToString());
                info.Title = dt.Rows[i]["title"].ToString();
                info.Parent = Convert.ToInt32(dt.Rows[i]["parent"].ToString());
                info.SortCode = Convert.ToInt32(dt.Rows[i]["sortcode"].ToString());
                info.Content = dt.Rows[i]["content"].ToString();
                infos[i] = info;
            }
            return infos;
        }
        return null;
    }

    public ItemInfo ReadItem(int id)
    {
        CItem item = new CItem();
        DataRow r = item.ReadItem(id);
        if (null != r)
        {
            ItemInfo info = new ItemInfo();
            info.ID = Convert.ToInt32(r["id"].ToString());
            info.Title = r["title"].ToString();
            info.Parent = Convert.ToInt32(r["parent"].ToString());
            info.SortCode = Convert.ToInt32(r["sortcode"].ToString());
            info.Content = r["content"].ToString();
            return info;
        }
        return null;
    }

    public NewsInfo ReadNews(int id)
    {
        CNews ns = new CNews();
        DataRow r = ns.ReadNews(id);
        if (null != r)
        {
            NewsInfo info = new NewsInfo();
            info.ID = Convert.ToInt32(r["id"].ToString());
            info.Title = r["title"].ToString();
            info.Parent = Convert.ToInt32(r["parent"].ToString());
            info.SortCode = Convert.ToInt32(r["sortcode"].ToString());
            info.Content = r["content"].ToString();
            info.DateTime = r["datetime"].ToString();
            return info;
        }
        return null;
    }

    public NewsInfo[] ReadNewses(int parent)
    {
        CNews ns = new CNews();
        DataTable dt = ns.ReadNewses(parent);
        if (null != dt)
        {
            NewsInfo[] infos = new NewsInfo[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                infos[i] = new NewsInfo()
                {
                    ID = Convert.ToInt32(dt.Rows[i]["id"].ToString()),
                    Title = dt.Rows[i]["title"].ToString(),
                    Parent = Convert.ToInt32(dt.Rows[i]["parent"].ToString()),
                    SortCode = Convert.ToInt32(dt.Rows[i]["sortcode"].ToString()),
                    Content = dt.Rows[i]["content"].ToString(),
                    DateTime = dt.Rows[i]["datetime"].ToString()
                };
            }
            return infos;
        }
        return null;
    }

    public ProductInfo ReadProduct(int id)
    {
        CProduct p = new CProduct();
        DataRow r = p.Read(id);
        if (null != r)
        {
            ProductInfo info = new ProductInfo()
            {
                ID = Convert.ToInt32(r["id"].ToString()),
                Title = r["title"].ToString(),
                Parent = Convert.ToInt32(r["parent"].ToString()),
                Image = r["image"].ToString(),
                Content = r["content"].ToString()
            };
            return info;
        }
        return null;
    }

    public ProductInfo[] ReadProducts(int parent)
    {
        List<ProductInfo> lst = new List<ProductInfo>();
        CProduct p = new CProduct();
        DataTable dt = p.ReadProducts(parent);
        if (null != dt)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ProductInfo info = new ProductInfo()
                {
                    ID = Convert.ToInt32(dt.Rows[i]["id"].ToString()),
                    Title = dt.Rows[i]["title"].ToString(),
                    Parent = Convert.ToInt32(dt.Rows[i]["parent"].ToString()),
                    Image = dt.Rows[i]["image"].ToString(),
                    Content = dt.Rows[i]["content"].ToString()
                };

                lst.Add(info);
            }
        }

        //子分类产品
        dt = p.ReadCategories(parent);
        if (null != dt)
        {
            foreach (DataRow r in dt.Rows)
            {
                lst.AddRange(ReadProducts(Convert.ToInt32(r["id"].ToString())));
            }
        }
        return lst.ToArray();
    }

    public ProductInfo[] ReadProductCategories(int parent)
    {
        CProduct p = new CProduct();
        DataTable dt = p.ReadCategories(parent);
        if (null != dt)
        {
            ProductInfo[] infos = new ProductInfo[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ProductInfo info = new ProductInfo()
                {
                    ID = Convert.ToInt32(dt.Rows[i]["id"].ToString()),
                    Title = dt.Rows[i]["title"].ToString(),
                    Parent = Convert.ToInt32(dt.Rows[i]["parent"].ToString()),
                    Image = dt.Rows[i]["image"].ToString(),
                    Content = dt.Rows[i]["content"].ToString()
                };

                #region Children
                info.Children = ReadProductCategories(info.ID);               
                #endregion

                infos[i] = info;
            }
            return infos;
        }
        return null;
    }

    public List<string[]> GetProductPath(int id)
    {
        List<string[]> lst = new List<string[]>();
        CProduct p = new CProduct();
        DataRow r = p.Read(id);
        lst.Add(new string[] { r["id"].ToString(),r["title"].ToString()});
        if (r["parent"].ToString() != "0")
        {
            lst.InsertRange(0, GetProductPath(Convert.ToInt32(r["parent"].ToString())));
        }
        return lst;
    }

    public FileInfor ReadFile(int id)
    {
        CFile f = new CFile();
        DataRow r = f.ReadFile(id);
        if (null != r)
        {
            FileInfor info = new FileInfor()
            {
                ID = Convert.ToInt32(r["id"].ToString()),
                Title = r["title"].ToString(),
                Parent = Convert.ToInt32(r["parent"].ToString()),
                Path = r["path"].ToString()
            };
            return info;
        }
        return null;
    }

    public FileInfor[] ReadFiles(int parent)
    {
        CFile f = new CFile();
        DataTable dt = f.ReadFiles(parent);
        if (null != dt)
        {
            FileInfor[] infos = new FileInfor[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                infos[i] = new FileInfor()
                {
                    ID = Convert.ToInt32(dt.Rows[i]["id"].ToString()),
                    Title = dt.Rows[i]["title"].ToString(),
                    Path = dt.Rows[i]["path"].ToString(),
                    Parent = Convert.ToInt32(dt.Rows[i]["parent"].ToString())
                };
            }
            return infos;
        }
        return null;
    }

    public IndexInfo ReadIndex(int title)
    {
        CIndex index = new CIndex();
        DataRow r = index.Read(title);
        if (null != r)
        {
            IndexInfo info = new IndexInfo()
            {
                ID = Convert.ToInt32(r["id"].ToString()),
                Path = r["path"].ToString(),
                Product = r["product"].ToString() == "" ? -1 : Convert.ToInt32(r["product"].ToString())
            };
            return info;
        }
        return null;
    }

    public bool Login(string name, string pwd)
    {
        CAgent a = new CAgent();
        return a.Login(name, pwd);
    }

    public CommInfo[] ReadCommonBySort(string sort)
    {
        CCommon comm = new CCommon();
        DataTable dt = comm.ReadBySort(sort);
        if (null != dt)
        {
            CommInfo[] infos = new CommInfo[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                infos[i] = new CommInfo()
                {
                    ID = Convert.ToInt32(dt.Rows[i]["id"].ToString()),
                    Title = dt.Rows[i]["title"].ToString(),
                    Content = dt.Rows[i]["content"].ToString()
                };
            }
            return infos;
        }
        return null;
    }
}
